﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProTrack.Api.Helper;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IServices.OA;
using ProTrack.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProTrack.Api.Controllers
{
    /// <summary>
    /// 员工设置
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeesController : ControllerBase
    {
        public LogHelper LogHelper { get; }

        public IEmployees Services { get; }

        public EmployeesController(LogHelper LogHelper, IEmployees services)
        {
            Services = services;
            this.LogHelper = LogHelper;
        }
        /// <summary>
        /// 获取所有员工设置
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll()
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var list = await Services.GetAllAsync();
                status.Data = list;
                status.Count = list.Count();
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("员工设置-获取所有", ex, HttpContext);
            }

            return Ok(status);
        }


        /// <summary>
        /// 分页获取员工设置
        /// </summary>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="PersonCode">员工编号</param>
        /// <param name="Name">员工姓名</param>
        /// <param name="IdCard">员工身份证</param>
        /// <returns></returns>

        [HttpGet]
        public async Task<IActionResult> Get([FromQuery] int PageIndex, [FromQuery] int PageSize, [FromQuery] string PersonCode, [FromQuery] string Name, [FromQuery] string IdCard)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                PersonCode = PersonCode ?? "";
                Name = Name ?? "";
                IdCard = IdCard ?? "";


                var list = await Services.GetAsync(PageIndex, PageSize, PersonCode, Name, IdCard);
                status.Data = list;
                status.Count = await Services.GetCountAsync(PersonCode, Name, IdCard);
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("员工设置-分页获取", ex, HttpContext);
            }

            return Ok(status);
        }

        /// <summary>
        /// 新增员工
        /// </summary>
        /// <param name="model">单位模型</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] Employees model)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                model.CreateTime = StringHelper.GetTimeNow();
                model.CreateBy = User.Identity.Name;              
                var isSuccess = await Services.AddAsync(model);
                if (isSuccess)
                {
                    status.Msg = "新增成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "新增失败，请检查数据。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("员工-新增", ex, HttpContext);
            }

            return Ok(status);
        }



        /// <summary>
        /// 修改员工设置
        /// </summary>
        /// <param name="model">单位模型</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Update([FromForm] Employees model)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                model.UpdateTime = StringHelper.GetTimeNow();
                model.UpdateBy = User.Identity.Name;
                var isSuccess = await Services.UpdateAsync(model);
                if (isSuccess)
                {
                    status.Msg = "修改成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "修改失败，请检查数据。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("员工设置-修改", ex, HttpContext);
            }

            return Ok(status);
        }


        /// <summary>
        /// 批量删除员工设置
        /// </summary>
        /// <param name="Ids">Id数组</param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll([FromForm] string Ids)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var arr = Ids.SplitId();
                var isSuccess = await Services.DeleteAsync(arr);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("员工设置-批量删除", ex, HttpContext);
            }

            return Ok(status);
        }



        /// <summary>
        /// 删除员工设置
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]

        public async Task<IActionResult> Del([FromForm] int Id)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var isSuccess = await Services.DeleteAsync(Id);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("员工设置-删除", ex, HttpContext);
            }

            return Ok(status);
        }
    }
}
